package com.jdd.modules.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.util.PasswordUtil;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.common.util.encryption.RASUtil;
import com.jdd.common.util.oConvertUtils;
import com.jdd.dao.entity.SysDepartArea;
import com.jdd.dao.mapper.SysDepartAreaMapper;
import com.jdd.modules.project.entity.SysProject;
import com.jdd.modules.project.mapper.SysProjectMapper;
import com.jdd.modules.project.service.SysProjectService;
import com.jdd.modules.project.vo.ProjectAddVO;
import com.jdd.modules.system.entity.SysRole;
import com.jdd.modules.system.entity.SysUser;
import com.jdd.modules.system.entity.SysUserDepart;
import com.jdd.modules.system.mapper.SysRoleMapper;
import com.jdd.modules.system.mapper.SysUserDepartMapper;
import com.jdd.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-15
 */
@Service
public class SysProjectServiceImpl extends ServiceImpl<SysProjectMapper, SysProject> implements SysProjectService {

    @Resource
    private SysProjectMapper sysProjectMapper;
    @Value("${user.default.password}")
    private String defaultPassword;
    @Value("${project.admin.role.code}")
    private String adminRoleCode;
    @Resource
    private SysDepartAreaMapper sysDepartAreaMapper;
    @Resource
    private SysUserDepartMapper sysUserDepartMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private ISysUserService sysUserService;

    @Override
    public IPage<SysProject> page(Page<SysProject> page, SysProject sysProject) {
        IPage<SysProject> sysProjectIPage = sysProjectMapper.getProjectPage(page, sysProject);

        return sysProjectIPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addProject(ProjectAddVO projectAddVO) throws Exception {
        SysUser user = projectAddVO.getSysUser();
        SysUser sameUser = sysUserService.getUserByName(user.getUsername());
        if (null != sameUser) {
            throw new Exception("管理员账户重复");
        }
        String orgCode = SnowFlakeUtil.getId();
        user.setPassword(defaultPassword);
        // 设置创建时间
        String salt = oConvertUtils.randomGen(8);
        user.setSalt(salt);
        String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt);
        user.setId(SnowFlakeUtil.getId());
        user.setPassword(passwordEncode);
        user.setStatus(1);
        user.setDelFlag("0");
        user.setOrgCode(orgCode);
        user.setSignature(RASUtil.sign(user.getUsername()));
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleLambdaQueryWrapper.eq(SysRole::getRoleCode, adminRoleCode);
        SysRole sysRole = sysRoleMapper.selectOne(sysRoleLambdaQueryWrapper);
        sysUserService.addUserWithRole(user, sysRole.getId());
        SysProject sysProject = projectAddVO.getSysProject();
        sysProject.setAdminUser(user.getUsername());
        sysProject.setAdminUserName(user.getRealname());
        sysProject.setProjectCode(orgCode);
        sysProjectMapper.insert(sysProject);
        SysDepartArea sysDepartArea = new SysDepartArea();
        sysDepartArea.setId(SnowFlakeUtil.getId());
        sysDepartArea.setName(sysProject.getProjectName());
        sysDepartArea.setSysProjectCode(orgCode);
        sysDepartAreaMapper.insert(sysDepartArea);
        SysUserDepart sysUserDepart = new SysUserDepart();
        sysUserDepart.setUserId(user.getId());
        sysUserDepart.setDepId(sysDepartArea.getId());
        sysUserDepartMapper.insert(sysUserDepart);
        return true;
    }

    @Override
    public void deleteData(String sysProjectId) {
        /*清空所有数据*/
        sysProjectMapper.deleteRolePermission(sysProjectId);
        /*删除用户角色*/
        sysProjectMapper.deleteSysRole(sysProjectId);

        /*删除用户与角色的关联关系*/
        sysProjectMapper.deleteUserRole(sysProjectId);

        /*删除项目下角色权限*/
        sysProjectMapper.deleteSysRolePermission(sysProjectId);

        /*删除大区域关联关系*/
        sysProjectMapper.deleteSysDepartArea(sysProjectId);

        /*删除大区域*/
        sysProjectMapper.deleteDepartArea(sysProjectId);

        /*删除小区域*/
        sysProjectMapper.deleteTArea(sysProjectId);

        /*删除楼栋*/
        sysProjectMapper.deleteBuilding(sysProjectId);

        /*删除项目下的部门*/
        sysProjectMapper.deleteDepartment(sysProjectId);

        /*删除项目下的设备*/
        sysProjectMapper.deleteDevice(sysProjectId);

        /*删除项目下设备的配置信息*/
        sysProjectMapper.deleteDeviceConf(sysProjectId);

        /*删除楼层*/
        sysProjectMapper.deleteFloor(sysProjectId);

        /*删除房屋*/
        sysProjectMapper.deleteHouse(sysProjectId);


        /*删除项目下的人员类型*/
        sysProjectMapper.deleteUserType(sysProjectId);


        /*删除项目下的用户*/
        sysProjectMapper.deleteUser(sysProjectId);

        /*删除项目*/
        sysProjectMapper.deleteProject(sysProjectId);


        /*删除单元*/
        sysProjectMapper.deleteUnit(sysProjectId);

        /*删除user和device的绑定关系*/
        sysProjectMapper.deleteUserDevice(sysProjectId);
    }
}
